package com.vim.modules.web.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.annotation.ParentModuleInfo;
import com.vim.common.model.response.PageRestfulResponse;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.utils.PageRestfulResponseUtils;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.ShopGoods;
import com.vim.modules.web.service.ShopGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@ParentModuleInfo(moduleCode = "shop", moduleName = "电商平台")
@Api(value = "商品管理", description = "获取商品列表和商品详情")
@RestController
@RequestMapping(value = "/api/shop/goods/")
public class ApiShopGoodsController {

    @Resource
    private ShopGoodsService shopGoodsService;

    @ApiOperation(value = "根据分类获取商品列表", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":[{\"goodsId\":\"1\",\"goodsName\":\"小辣椒 红辣椒R15 全面屏双摄拍照手机 6+128GB 黑色 全网通4G 双卡双待 大屏安卓手机京东自营\",\"goodsTitle\":\"6.2英寸屏|红外人像识别解锁|无线充电，前置1600万AI美颜后置1600万+500万AI双摄\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t23131/54/1346507459/233648/178a183a/5b5af8d3N0fed8584.jpg!q80.jpg.webp\",\"goodsBody\":null,\"goodsPrice\":1899.99,\"goodsPriceStr\":\"1899.99\",\"classifyId\":\"1\",\"classifyName\":\"手机数码\"},{\"goodsId\":\"2\",\"goodsName\":\"Apple iPhone 8 Plus 64GB 红色特别版 移动联通电信4G手机\",\"goodsTitle\":\"好物尽享，美在智慧，炎炎八月，愈发精彩。\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t17383/125/1470478028/71155/1cb53bc2/5acc5248N6a5f81cd.jpg!q80.jpg.webp\",\"goodsBody\":null,\"goodsPrice\":5799.00,\"goodsPriceStr\":\"5799.00\",\"classifyId\":\"1\",\"classifyName\":\"手机数码\"},{\"goodsId\":\"3\",\"goodsName\":\"荣耀10  GT游戏加速 AIS手持夜景  6GB+64GB  幻影蓝全网通 移动联通电信4G 双卡双待\",\"goodsTitle\":\"限时优惠200,成交价2399！荣耀10GT，游戏加速！抢818神券尽在818手机节，8.3-8.20全面开启！\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t18157/222/1822300674/231514/6c179af8/5ad87390N086a3c91.jpg!q80.jpg.webp\",\"goodsBody\":null,\"goodsPrice\":2399.00,\"goodsPriceStr\":\"2399.00\",\"classifyId\":\"1\",\"classifyName\":\"手机数码\"}],\"totalCount\":3,\"totalPage\":1,\"currentPage\":0,\"pageSize\":100}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true),
                        @ApiImplicitParam(name = "limit", value = "每页显示数量", required = true),
                        @ApiImplicitParam(name = "classifyId", value = "分类ID", required = true)})
    @RequestMapping(value = "/goodsList")
    public PageRestfulResponse goodsList(@RequestParam(value = "classifyId") String classifyId,
                                         @RequestParam(value = "page") Integer page,
                                         @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotBlank(classifyId);
        PageHelper.startPage(page,limit);
        ShopGoods goods = new ShopGoods();
        goods.setClassifyId(classifyId);
        List<ShopGoods> list = shopGoodsService.findList(goods);
        PageInfo<ShopGoods> pageInfo = new PageInfo<>(list);
        return PageRestfulResponseUtils.handle(pageInfo);
    }

    @ApiOperation(value = "获取商品详情", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"goodsId\":\"1\",\"goodsName\":\"小辣椒 红辣椒R15 全面屏双摄拍照手机 6+128GB 黑色 全网通4G 双卡双待 大屏安卓手机京东自营\",\"goodsTitle\":\"6.2英寸屏|红外人像识别解锁|无线充电，前置1600万AI美颜后置1600万+500万AI双摄\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t23131/54/1346507459/233648/178a183a/5b5af8d3N0fed8584.jpg!q80.jpg.webp\",\"goodsBody\":null,\"goodsPrice\":1899.99,\"goodsPriceStr\":\"1899.99\",\"classifyId\":\"1\",\"classifyName\":\"手机数码\"}}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true)})
    @RequestMapping(value = "/goodsDetail")
    public RestfulResponse goodsDetail(@RequestParam(value = "goodsId") String goodsId){
        ValidateUtils.checkNotBlank(goodsId);
        return new RestfulResponse(shopGoodsService.find(goodsId));
    }
}
